package com.dty.common.utils;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.jms.*;
import java.util.Map;

@Component
public class ActiveMQutil {

	public static ActiveMQutil activeMQutil;

	private ConnectionFactory connectionFactory;

	@Value("${amqConfig.amqUrl}")
	private String amqUrl;

	// 创建AMQ生产工厂
	@PostConstruct
	public void init() {
		activeMQutil = this;
		// amq 配置信息
		String AMQ_USER = "admin";
		String AMQ_PWD = "admin";
//		String AMQ_URL = "tcp://127.0.0.1:61616";
		String AMQ_URL = amqUrl;
//		String AMQ_URL = "tcp://10.27.3.7:61616";
		// 创建会话总线
		activeMQutil.connectionFactory = new ActiveMQConnectionFactory(AMQ_USER, AMQ_PWD, AMQ_URL);
	}

	// 获取生产工厂
	public static ConnectionFactory getAmqFactory() {

		return activeMQutil.connectionFactory;

	}

	/**
	 * 发送消息
	 * 
	 * @param queue    队列名称
	 * @param infoMap  信息内容
	 * @param uniqueId 唯一标识
	 * @param cjqNo    采集器信息
	 * @throws JMSException
	 */
	public static void SendMsg(String queue, Map<String, Object> infoMap, String uniqueId, String cjqNo)
			throws JMSException {

		// 创建会话连接
		Connection connection = activeMQutil.connectionFactory.createConnection();
		connection.start();
		// 创建Session
		Session session = connection.createSession(Boolean.TRUE.booleanValue(), Session.AUTO_ACKNOWLEDGE);
		Destination destination = session.createQueue(queue);
		// 信息生产者
		MessageProducer producer = session.createProducer(destination);
		producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

		// 生成信息
		MapMessage mapMsg = session.createMapMessage();
		mapMsg.setJMSCorrelationID(uniqueId);
//		mapMsg.setString("groupId", uniqueId);
		// mapMsg.setStringProperty("cjqNo", cjqNo);
		for (String s : infoMap.keySet()) {
			if (infoMap.get(s) instanceof String) {
				mapMsg.setString(s, (String) infoMap.get(s));
			} else if (infoMap.get(s) instanceof Integer) {
				mapMsg.setInt(s, (Integer) infoMap.get(s));
			} else if (infoMap.get(s) instanceof Double) {
				mapMsg.setDouble(s, (Double) infoMap.get(s));
			}
		}
		// 发送消息
		producer.send(mapMsg);
		session.commit();

		// 关闭连接
		producer.close();
		session.close();
		connection.close();

	}

	/**
	 * 接收消息
	 * @param Queue 队列名称
	 * @param uniqueId 唯一标识
	 * @param timeout 等待时间
	 * @return MapMessage
	 * @throws JMSException
	 */
	public static MapMessage ReceiveMsg(String Queue, String uniqueId, long timeout) throws JMSException {
		
		// 创建会话连接
		Connection connection = activeMQutil.connectionFactory.createConnection();
		connection.start();
		// 创建Session
		Session session = connection.createSession(Boolean.TRUE.booleanValue(), Session.CLIENT_ACKNOWLEDGE);
		Destination destination = session.createQueue(Queue);
		// 信息消费者
		MessageConsumer consumer = session.createConsumer(destination, "groupId=\'" + uniqueId + "\'");
		MapMessage msg = (MapMessage) consumer.receive(timeout);
		session.commit();

		// 关闭连接
		consumer.close();
		session.close();
		connection.close();
		
		return msg;
		
	}

}
